package com.yixin.hospital.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.entity.domain.PatientAddress;
import com.yixin.hospital.entity.dto.req.patientaddress.PatientAddressAddReqDTO;
import com.yixin.hospital.entity.dto.req.patientaddress.PatientAddressEditReqDTO;
import com.yixin.hospital.service.PatientAddressService;
import com.yixin.hospital.mapper.PatientAddressMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author Administrator
* @description 针对表【patient_address(患者住址表)】的数据库操作Service实现
* @createDate 2024-09-10 15:38:14
*/
@Service
public class PatientAddressServiceImpl extends ServiceImpl<PatientAddressMapper, PatientAddress>
    implements PatientAddressService{

    @Override
    public ApiResult<?> add(PatientAddressAddReqDTO patientAddressAddReqDTO, String userName) {
        for (PatientAddress patientAddressReq : patientAddressAddReqDTO.getAddressList()) {
            PatientAddress patientAddress = new PatientAddress();
            BeanUtils.copyProperties(patientAddressReq, patientAddress);
            patientAddress.setUpdatedBy(userName);
            if (Objects.isNull(patientAddressReq.getId())) {
                patientAddress.setCreatedBy(userName);
            }
            this.saveOrUpdate(patientAddress);
        }
        return ApiResult.success();
    }

    @Override
    public ApiResult<?> edit(PatientAddressEditReqDTO patientAddressEditReqDTO, String userName) {
        PatientAddress patientAddress = new PatientAddress();
        BeanUtils.copyProperties(patientAddressEditReqDTO, patientAddress);
        patientAddress.setUpdatedBy(userName);
        this.updateById(patientAddress);
        return ApiResult.success();
    }
}




